package com.task;

import com.controller.TestController;
import com.mapper.entity.StockCirculation;
import com.mapper.entity.StockCirculationDetail;
import com.mapper.entity.StockCirculationDetailOrg;
import com.mapper.entity.mapper.StockCirculationDetailMapper;
import com.mapper.entity.mapper.StockCirculationMapper;
import com.service.impl.HandleStockOrg;
import lombok.Data;
import lombok.SneakyThrows;
import tk.mybatis.mapper.weekend.Weekend;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: create by God of SJF
 * @version: v1.0
 * @description: com.task
 * @date:2021/9/18
 */

@Data
public class InsertStockAndDetailTask implements Runnable {


    StockCirculationMapper stockCirculationMapper;

    StockCirculationDetailMapper stockCirculationDetailMapper;

    HandleStockOrg handleStockOrg;

    List<String> list;

    File errorFile;

    @SneakyThrows
    @Override
    public void run() {
        for (String string : list) {
            try {
                StockCirculationDetailOrg wrap = handleStockOrg.wrap(string);
                StockCirculation stockCirculation = handleStockOrg.Instance(wrap);
                stockCirculationMapper.insertReturnId(stockCirculation);
                StockCirculationDetail stockCirculationDetail = handleStockOrg.InstanceDetail(wrap);
                stockCirculationDetailMapper.insertSelectiveReturnId(stockCirculationDetail);
                Weekend<StockCirculation> stockCirculationWeekend2 = Weekend.of(StockCirculation.class);
                stockCirculationWeekend2.createCriteria().andEqualTo("barcodeContent", stockCirculation.getBarcodeContent()).andEqualTo("id", stockCirculation.getId());
                stockCirculation.setLinkId(stockCirculationDetail.getLinkId());
                stockCirculationMapper.updateByExampleSelective(stockCirculation, stockCirculationWeekend2);

//                StockCirculationDetailOrg wrap = handleStockOrg.wrap(string);
//                StockCirculation stockCirculation = handleStockOrg.Instance(wrap);
//                stockCirculationMapper.insertReturnId(stockCirculation);
//                StockCirculationDetail stockCirculationDetail = handleStockOrg.InstanceDetail(wrap);
//                stockCirculationDetailMapper.insertSelectiveReturnId(stockCirculationDetail);
//                Weekend<StockCirculation> stockCirculationWeekend2 = Weekend.of(StockCirculation.class);
//                stockCirculationWeekend2.createCriteria().andEqualTo("barcodeContent", stockCirculation.getBarcodeContent()).andEqualTo("id", stockCirculation.getId());
//                stockCirculation.setLinkId(stockCirculationDetail.getLinkId());
//                stockCirculationMapper.updateByExampleSelective(stockCirculation, stockCirculationWeekend2);
            } catch (Exception e) {
                List<String> list = new ArrayList<>();
                list.add(string + "\r\n");
                TestController.writeFile(errorFile.getAbsolutePath(), list);
                continue;
            }
        }
    }

}
